﻿using System;
using MVVM.Northwind.Employee.DataAccess;
using MVVM.Northwind.Employee.Commands;
using System.Data.Linq;
using System.Windows.Input;
using MVVM.Northwind.Employee.Services;

namespace MVVM.Northwind.Employee.ViewModel
{
    public class EmployeeViewModel:WorkspaceViewModel
    {
        private ICommand _addEmployee;
        private ICommand _cancelAddEmployee;

        public EmployeeViewModel()
        {
            
        }

        public int EmployeeID { get;  set; }
        public string LastName { get;  set; }
        public string FirstName { get;  set; }
        public string Designation { get;  set; }
        public string TitleOfCourtesy { get;  set; }
        public DateTime BirthDate { get;  set; }
        public DateTime HireDate { get;  set; }
        public string Address { get;  set; }
        private string City { get;  set; }
        private string Region { get;  set; }
        private string PostalCode { get;  set; }
        private string Country { get;  set; }
        private string HomePhone { get;  set; }
        private string Extension { get; set; }
        private Binary Photo { get; set; }

        public ICommand AddEmployee
        {
            get
            {
                if (_addEmployee!=null)
                {
                    _addEmployee = new MVVMRoutedCommand(
                        (param) => this.CreateEmployee(), (param) => this.CanCreateEmployee());
                }
                return _addEmployee;
            }
        }

        private void CreateEmployee()
        {
            DataAccess.Employee emp = new DataAccess.Employee();
            emp.EmployeeID = this.EmployeeID;
            emp.LastName = this.LastName;
            emp.FirstName = this.FirstName;
            emp.Title = this.Designation;
            emp.BirthDate = this.BirthDate;
            emp.HireDate = this.HireDate;
            emp.Address = this.Address;
            emp.City = this.City;
            emp.Region = this.Region;
            emp.PostalCode = this.PostalCode;
            emp.Country = this.Country;
            emp.HomePhone = this.HomePhone;
            emp.Extension = this.Extension;
            emp.Photo = this.Photo;
            DatabaseGateway dbGwy = DatabaseGateway.CreateInstance();
            dbGwy.AddEmployee(emp);
        }

        private bool CanCreateEmployee()
        {
            return true;
        }

        void Saved()
        {
            var msgBox = base.GetService<IMessageBoxService>();
            if (msgBox != null)
            {
                msgBox.Show("Your record has been added", "Add Data", 
                    System.Windows.MessageBoxButton.OK,System.Windows.MessageBoxImage.Information);
            }
        }
    }
}
